using System;
using System.Collections.Generic;
using System.Text;
using QLHXNK.DataAccess;
using System.Data;
using System.Windows.Forms;

namespace QLHXNK.BusinessLogic
{
    class BaoCao
    {
        public static DataTable getImportNoteReport(DateTime fromDate, DateTime toDate, int itemID)
        {
            return BaoCaoData.getImportNoteReport(fromDate, toDate, itemID);
        }

        private static DataTable getReturnTableForExpensesReport(DataTable table)
        {
            try
            {
                DataTable ret = new DataTable();
                Double d = 1;
                int k = 1;
                
                ret.Columns.Add("thang", k.GetType());
                ret.Columns.Add("nam", k.GetType());
                ret.Columns.Add("thangnam");
                ret.Columns.Add("date", DateTime.Today.GetType());
                ret.Columns.Add("tiennha", d.GetType());
                ret.Columns.Add("luongnv", d.GetType());
                ret.Columns.Add("vpp", d.GetType());
                ret.Columns.Add("diennuoc", d.GetType());
                ret.Columns.Add("khac", d.GetType());
                ret.Columns.Add("tong", d.GetType());


                int luongnvID = BaoCao.getExpensesID("TL");
                int tiennhaID = BaoCao.getExpensesID("TN");
                int vppID = BaoCao.getExpensesID("VPP");
                int diennuocID = BaoCao.getExpensesID("DN");

                for (int i = 0; i < table.Rows.Count; i++)
                {
                    if ((int)table.Rows[i]["month_name"] != -1 && (int)table.Rows[i]["year_name"] != -1)
                    {
                        DataRow row = ret.NewRow();
                        Double khac = 0;
                        Double tong = 0;

                        row["thang"] = table.Rows[i]["month_name"];
                        row["nam"] = table.Rows[i]["year_name"];
                        row["thangnam"] = row["thang"].ToString() + "/" + row["nam"].ToString();
                        try
                        {
                            row["date"] = DateTime.Parse(row["thangnam"].ToString());
                            //MessageBox.Show(((DateTime)row["date"]).Month.ToString() +"/"+ ((DateTime)row["date"]).Year.ToString());
                        }
                        catch
                        {
                            
                        }
                        //
                        for (int j = i; j < table.Rows.Count; j++)
                        {
                            if ((int)table.Rows[j]["month_name"] == (int)row["thang"] && (int)table.Rows[j]["year_name"] == (int)row["nam"])
                            {
                                try
                                {
                                    bool flag = false;
                                    if ((int)table.Rows[j]["expenses_id"] == luongnvID)
                                    {
                                        row["luongnv"] = table.Rows[j]["amount"];

                                        flag = true;
                                    }

                                    if ((int)table.Rows[j]["expenses_id"] == tiennhaID)
                                    {
                                        row["tiennha"] = table.Rows[j]["amount"];
                                        flag = true;
                                    }

                                    if ((int)table.Rows[j]["expenses_id"] == vppID)
                                    {
                                        row["vpp"] = table.Rows[j]["amount"];
                                        flag = true;
                                    }

                                    if ((int)table.Rows[j]["expenses_id"] == diennuocID)
                                    {
                                        row["diennuoc"] = table.Rows[j]["amount"];
                                        flag = true;
                                    }


                                    if (flag == false)
                                    {
                                        try
                                        {
                                            khac += Double.Parse(table.Rows[j]["amount"].ToString());
                                        }
                                        catch
                                        {
                                        }
                                    }

                                    try
                                    {
                                        tong += Double.Parse(table.Rows[j]["amount"].ToString());
                                    }
                                    catch
                                    {
                                    }
                                }
                                catch
                                {
                                }

                                table.Rows[j]["month_name"] = -1;
                                table.Rows[j]["year_name"] = -1;

                            }
                        }
                        if (khac > 0)
                            row["khac"] = khac;

                        if (tong > 0)
                            row["tong"] = tong;

                        table.Rows[i]["month_name"] = -1;
                        table.Rows[i]["year_name"] = -1;

                        //add new row:
                        ret.Rows.Add(row);
                    }
                }

                //sort by "date":
                try
                {
                    DataView v = ret.DefaultView;
                    v.Sort = "date ASC";
                    return v.ToTable();
                }
                catch
                {
                    return ret;
                }

            }
            catch
            {
                return null;
            }
        }

        public static DataTable getExpensesReport(String fromMonth, String fromYear, String toMonth, String toYear)
        {
            try
            {
                DataTable table = BaoCaoData.getExpensesReport(fromMonth, fromYear, toMonth, toYear);
                return getReturnTableForExpensesReport(table);          
            }
            catch
            {
                return null;
            }
        }

        public static int getExpensesID(String code)
        {
            try
            {
                DataTable table = ExspencesData.GetExpenses(code, -1);
                if (table != null && table.Rows.Count > 0)
                    return (int)table.Rows[0]["ID"];
                return -1;
            }
            catch
            {
                return -1;
            }

        }

        public static DataTable getItemByID(int ItemID)
        {
           return BaoCaoData.getItemByID(ItemID);
        }
    }
}
